<template>
  <view class="loginson">
    <u--form :labelWidth="60" :rules="rules" :model="form" ref="uForm">
      <u-form-item label="手机号" prop="phone">
        <u-input type="number" v-model="form.phone" />
      </u-form-item>

      <u-form-item label="密码" prop="password">
        <u-input v-model="form.password" />
      </u-form-item>

      <u-form-item label="验证码" prop="yzm">
        <u-input type="number" v-model="form.yzm" />
        <u-button
          @tap="getCode"
          :text="tips"
          type="success"
          size="mini"
          style="flex: 0.5"
          :disabled="disabled1"
        ></u-button>
      </u-form-item>
      <view style="color: greenyellow;" v-if="!yzmnum == ''">验证码为：{{ yzmnum }}</view>
    </u--form>
    <u-code
      ref="uCode"
      @change="codeChange"
      seconds="20"
      @start="disabled1 = true"
      @end="disabled1 = false"
      style="height: 100%"
    ></u-code>
    <u-button @click="submit">注册</u-button>
  </view>
</template>

<script>
import { getcode, register } from "../../request/API/login";
export default {
  data() {
    return {
      msg:'',
      yzmnum: "",
      disabled1: false,
      tips: "",
      form: {
        phone: "",
        password: "",
        yzm: "",
      },
      rules: {
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: ["blur", "change"],
          },
          {
            // 自定义验证函数，见上说明
            validator: (rule, value, callback) => {
              // 上面有说，返回true表示校验通过，返回false表示不通过
              // uni.$u.test.mobile()就是返回true或者false的
              return uni.$u.test.mobile(value);
            },
            message: "请输入正确的手机号",
            // 触发器可以同时用blur和change
            trigger: ["change", "blur"],
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur", "change"],
          },
          // 正则判断为字母或数字
          {
            pattern: /^[0-9a-zA-Z]*$/g,
            // 正则检验前先将值转为字符串
            transform(value) {
              return String(value);
            },
            message: "只能包含字母或数字",
          },
          // 6-8个字符之间的判断
          {
            min: 6,
            max: 8,
            message: "请输入6-8位包含字母数字的密码",
          },
        ],
        yzm: [
          {
            required: true,
            message: "请输入验证码",
            trigger: ["blur", "change"],
          },
          {
            // 自定义验证函数，见上说明
            validator: (rule, value, callback) => {
              // 上面有说，返回true表示校验通过，返回false表示不通过
              // uni.$u.test.mobile()就是返回true或者false的
              return uni.$u.test.code("4567", 4);
            },
            message: "请输入6位验证码",
            // 触发器可以同时用blur和change
            trigger: ["change", "blur"],
          },
        ],
      },
    };
  },
  methods: {
    submit() {
      this.$refs.uForm
        .validate()
        .then((res) => {
          register({
            phone: this.form.phone,
            password: this.form.password,
            captcha: this.form.yzm,
          }).then((res) => {
            // console.log(res.msg);
            this.msg = res.msg;
            uni.$u.toast(this.msg);
            if (this.msg == "已注册") {
             uni.showModal({
              title: this.msg,
              content: '是否前往登录？',
              success: function (res) {
                if (res.confirm) {
                  uni.navigateBack({
                    delta: 1,
                  });
                  // console.log('用户点击确定');
                } else if (res.cancel) {
                  // console.log('用户点击取消');
                }
              }
             });
            }else if(this.msg == "注册成功"){
              uni.showModal({
              title: this.msg,
              content: '是否前往登录？',
              success: function (res) {
                if (res.confirm) {
                  uni.navigateBack({
                    delta: 1,
                  });
                  // console.log('用户点击确定');
                } else if (res.cancel) {
                  // console.log('用户点击取消');
                }
              }
             });
            }else if(this.msg == "验证码不对"){
              uni.$u.toast(this.msg);
            }
          });
          uni.$u.toast("校验通过");
          // uni.navigateBack({
          //   delta: 1,
          // });
        })
        .catch((errors) => {
          uni.$u.toast("校验失败");
        });
    },
    codeChange(text) {
      this.tips = text;
    },
    getCode() {
      if (this.form.phone == "") {
        uni.$u.toast("请输入手机号");
        return;
      } else {
        if (this.$refs.uCode.canGetCode) {
          // 模拟向后端请求验证码
          uni.showLoading({
            title: "正在获取验证码",
          });
          setTimeout(() => {
            uni.hideLoading();
            // 这里此提示会被this.start()方法中的提示覆盖
            uni.$u.toast("验证码已发送");
            // 通知验证码组件内部开始倒计时
            this.$refs.uCode.start();

            getcode({
            phone: this.form.phone,
          }).then((res) => {
            // console.log(res.data);
            this.yzmnum = res.data;
          });
          }, 2000);
         
        } else {
          uni.$u.toast("倒计时结束后再发送");
        }
      }
    },
  },
};
</script>


<style lang="scss">
.loginson {
  width: 94%;
  margin: auto;
}
</style>
